<?php

/**
 * invoice actions.
 *
 * @package    dashboard
 * @subpackage invoice
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class invoiceActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->invoices = new Invoice();

    $this->pager = new sfDoctrinePager('Invoice',sfConfig::get('app_max_invoices'));
    $this->pager->setQuery($this->invoices->getInvoicesQuery($this->getUser()->getAttribute('account_id')));
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();

    $this->invoices = $this->pager->getResults();

    $this->account = Doctrine_Core::getTable('Account')->find($this->getUser()->getAttribute('account_id'));

    $this->servicesTons = Invoice::getTonsRow(Invoice::getInvoicesIds($this->getUser()->getAttribute('account_id')));

    //$childs = Doctrine_Core::getTable('Account')->getChildsAccountArray($this->getUser()->getAttribute('account_id'));
    $chartRecycle = Doctrine_Core::getTable('Account')->getChartMonthsTons(6,"Recycle",$this->getUser()->getAttribute('account_id'));
    //$this->strXML = "<chart caption='Daily Visits' subcaption='(from 02/02/2006 to 05/02/2006)' lineThickness='1' showValues='0' formatNumberScale='0' anchorRadius='2' divLineAlpha='20' divLineColor='CC3300' divLineIsDashed='1' showAlternateHGridColor='1' alternateHGridAlpha='5' alternateHGridColor='CC3300' shadowAlpha='40' labelStep='2' numvdivlines='5' chartRightMargin='35' bgColor='FFFFFF,CC3300' bgAngle='270' bgAlpha='10,10'><set label='Jan' value='462' /><set label='Feb' value='857' /><set label='Mar' value='671' /></chart>";
    $chartTrash = Doctrine_Core::getTable('Account')->getChartMonthsTons(6,"Trash",$this->getUser()->getAttribute('account_id'));
    
    $this->strXML = "<chart caption='Tons per month' subcaption='(from ".date('m/d/Y', strtotime(date('Y').'/'.date('m').'/01 -5 month'))." to ".date('m/d/Y').")' xAxisName='Months' yAxisName='Tons' lineThickness='1' showValues='0' formatNumberScale='0' anchorRadius='2' divLineAlpha='20' divLineColor='CC3300' divLineIsDashed='1' showAlternateHGridColor='1' alternateHGridAlpha='5' alternateHGridColor='000000' shadowAlpha='40' labelStep='1' numvdivlines='4' chartRightMargin='35' bgColor='FFFFFF,000000' bgAngle='270' bgAlpha='10,10'>";

    $this->strXML .= "<categories>";
    for ($i=6; $i > 0; $i--) :
        $this->strXML .= "<category label='".date('M/Y', strtotime(date("Y-m-d"). '-'.($i-1).' month'))."'/>";
    endfor;

    $this->strXML .= "</categories>";
    // Trash
    $this->strXML .= "<dataset seriesName='Trash Volume' color='1D8BD1' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'>";
    foreach ($chartTrash as $child) :
        $this->strXML .= "<set value='".number_format($child,2,'.','')."'/>";
    endforeach;
    $this->strXML .= "</dataset>";

    $this->strXML .= "<dataset seriesName='Recycle Volume' color='F1683C' anchorBorderColor='F1683C' anchorBgColor='F1683C'>";
    foreach ($chartRecycle as $child) :
        $this->strXML .= "<set value='".number_format($child,2,'.','')."'/>";
    endforeach;
    $this->strXML .= "</dataset>";

    $chartCorporateTrash = Doctrine_Core::getTable('Account')->getChartMonthsTons2(6,"Trash");
    $this->strXML .= "<dataset seriesName='Corporate Trash Volume' color='F1ACAC' anchorBorderColor='F1ACAC' anchorBgColor='F1ACAC'>";

    $arrayMonths = Rubicon::arrayMonths(6);
    $i = 0;
    foreach ($chartCorporateTrash as $child) :
        if ($child->getMes() != $arrayMonths[$i]) :

            while (($arrayMonths[$i] != $child->getMes()) && ($i < 6)) :
                $this->strXML .= "<set value='".number_format(0,2,'.','')."'/>";
                $i++;
            endwhile;
        endif;

        $this->strXML .= "<set value='".number_format($child->getSuma(),2,'.','')."'/>";
        $i++;
    endforeach;
    //else :
    if ($i < 6) :
        while ($i < 6) :
            $this->strXML .= "<set value='".number_format(0,2,'.','')."'/>";
            $i++;
        endwhile;
    endif;

    /*foreach ($chartCorporateTrash as $child) :
        $this->strXML .= "<set value='".number_format($child,2,'.','')."'/>";
    endforeach;*/
    $this->strXML .= "</dataset>";

    $this->strXML .= "<styles><definition><style name='CaptionFont' type='font' size='12'/></definition><application><apply toObject='CAPTION' styles='CaptionFont'/><apply toObject='SUBCAPTION' styles='CaptionFont'/></application></styles>";
    $this->strXML .= "</chart>";
  }

  public function executeList(sfWebRequest $request)
  {
    $this->forward404unless($request->isXmlHttpRequest());

    $page = intval($request->getParameter("page"));

    $invoices = new Invoice();

    $this->pager = new sfDoctrinePager('Invoice',6);
    $this->pager->setQuery($invoices->getInvoicesQuery($this->getUser()->getAttribute('account_id')));
    $this->pager->setPage($request->getParameter('page', $page));
    $this->pager->init();

    $invoices = $this->pager->getResults();

    return $this->renderPartial('invoice/list',
            array('page' => $page,
                  'invoices' => $invoices,
                  'pager' => $this->pager));
  }

  public function executeLocationList(sfWebRequest $request)
  {
    $this->forward404unless($request->isXmlHttpRequest());

    $page = intval($request->getParameter("page"));

    $account = Doctrine_Core::getTable('Account')->find($this->getUser()->getAttribute('account_id'));

    $locations = new Account();

    $this->pager = new sfDoctrinePager('Account',7);
    $this->pager->setQuery($locations->getChildsAccountQuery($this->getUser()->getAttribute('account_id')));
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();

    $locations = $this->pager->getResults();

    return $this->renderPartial('invoice/locationList',
            array('page' => $page, 'account' => $account,
                  'locations' => $locations,
                  'pager' => $this->pager));
  }

  public function executeLocationsChart(sfWebRequest $request)
  {
    //$this->forward404unless($request->isXmlHttpRequest());

    $month = intval($request->getParameter("month"));
    $my = intval($request->getParameter("my"));
    $corporate = intval($request->getParameter("corporate"));
    $childs = explode(",",$request->getParameter("childs"));

    $this->strXML = "<chart caption='Tons per month' subcaption='(from ".date('m/d/Y', strtotime(date('Y').'/'.date('m').'/01 -'.($month-1).' month'))." to ".date('m/d/Y').")' xAxisName='Months' yAxisName='Tons' lineThickness='1' showValues='0' formatNumberScale='0' anchorRadius='2' divLineAlpha='20' divLineColor='CC3300' divLineIsDashed='1' showAlternateHGridColor='1' alternateHGridAlpha='".($month-1)."' alternateHGridColor='000000' shadowAlpha='40' labelStep='1' numvdivlines='".($month-2)."' chartRightMargin='35' bgColor='FFFFFF,000000' bgAngle='270' bgAlpha='10,10'>";

    $this->strXML .= "<categories>";
    for ($i=$month; $i > 0; $i--) :
        $this->strXML .= "<category label='".date('M/Y', strtotime(date("Y-m-d"). '-'.($i-1).' month'))."'/>";
    endfor;

    $this->strXML .= "</categories>";
    // Locations stats
    if ($my == 1) :
    if ($childs[0]==null) :
    $childs = Doctrine_Core::getTable('Account')->getChildsAccountArray($this->getUser()->getAttribute('account_id'));
    endif;
    $this->strXML .= "<dataset seriesName='Trash Volume' color='1D8BD1' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'>";

    $chartRecycle = Doctrine_Core::getTable('Account')->getChartMonthsTons($month,"Recycle",$childs);
    $chartTrash = Doctrine_Core::getTable('Account')->getChartMonthsTons($month,"Trash",$childs);
        // Trash Locations
        foreach ($chartTrash as $child) :
            $this->strXML .= "<set value='".number_format($child,2,'.','')."'/>";
        endforeach;
        $this->strXML .= "</dataset>";
        // Recycle Locations
        $this->strXML .= "<dataset seriesName='Recycle Volume' color='F1683C' anchorBorderColor='F1683C' anchorBgColor='F1683C'>";
        foreach ($chartRecycle as $child) :
            $this->strXML .= "<set value='".number_format($child,2,'.','')."'/>";
        endforeach;
        $this->strXML .= "</dataset>";
    endif;
    
    // Trash Corporate
    if ($corporate == 1) :
        $chartCorporateTrash = Doctrine_Core::getTable('Account')->getChartMonthsTons($month,"Trash");
        $this->strXML .= "<dataset seriesName='Corporate Trash Volume' color='F1ACAC' anchorBorderColor='F1ACAC' anchorBgColor='F1ACAC'>";
        foreach ($chartCorporateTrash as $child) :
            $this->strXML .= "<set value='".number_format($child,2,'.','')."'/>";
        endforeach;
        $this->strXML .= "</dataset>";
    endif;

    $this->strXML .= "<styles><definition><style name='CaptionFont' type='font' size='12'/></definition><application><apply toObject='CAPTION' styles='CaptionFont'/><apply toObject='SUBCAPTION' styles='CaptionFont'/></application></styles>";
    $this->strXML .= "</chart>";

    return $this->renderPartial('invoice/locationsChart',
            array('month' => $month, 'strXML' => $this->strXML,
                  'my' => $my, 'corporate' => $corporate  ));
  }

  public function executeIndividualChart(sfWebRequest $request)
  {
    $this->forward404unless($request->isXmlHttpRequest());

    $month = intval($request->getParameter("month"));
    $my = intval($request->getParameter("my"));
    $corporate = intval($request->getParameter("corporate"));

    $this->strXML = "<chart caption='Tons per month' subcaption='(from ".date('m/d/Y', strtotime(date('Y').'/'.date('m').'/01 -'.($month-1).' month'))." to ".date('m/d/Y').")' xAxisName='Months' yAxisName='Tons' lineThickness='1' showValues='0' formatNumberScale='0' anchorRadius='2' divLineAlpha='20' divLineColor='CC3300' divLineIsDashed='1' showAlternateHGridColor='1' alternateHGridAlpha='".($month-1)."' alternateHGridColor='000000' shadowAlpha='40' labelStep='1' numvdivlines='".($month-2)."' chartRightMargin='35' bgColor='FFFFFF,000000' bgAngle='270' bgAlpha='10,10'>";

    $this->strXML .= "<categories>";
    for ($i=$month; $i > 0; $i--) :
        $this->strXML .= "<category label='".date('M/Y', strtotime(date("Y-m-d"). '-'.($i-1).' month'))."'/>";
    endfor;

    $this->strXML .= "</categories>";
    // Locations stats
    if ($my == 1) :
    //$childs = Doctrine_Core::getTable('Account')->getChildsAccountArray($this->getUser()->getAttribute('account_id'));

    $this->strXML .= "<dataset seriesName='Trash Volume' color='1D8BD1' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'>";

    $chartRecycle = Doctrine_Core::getTable('Account')->getChartMonthsTons($month,"Recycle",$this->getUser()->getAttribute('account_id'));
    $chartTrash = Doctrine_Core::getTable('Account')->getChartMonthsTons($month,"Trash",$this->getUser()->getAttribute('account_id'));
        // Trash Locations
        foreach ($chartTrash as $child) :
            $this->strXML .= "<set value='".number_format($child,2,'.','')."'/>";
        endforeach;
        $this->strXML .= "</dataset>";
        // Recycle Locations
        $this->strXML .= "<dataset seriesName='Recycle Volume' color='F1683C' anchorBorderColor='F1683C' anchorBgColor='F1683C'>";
        foreach ($chartRecycle as $child) :
            $this->strXML .= "<set value='".number_format($child,2,'.','')."'/>";
        endforeach;
        $this->strXML .= "</dataset>";
    endif;

    // Trash Corporate
    if ($corporate == 1) :
        $chartCorporateTrash = Doctrine_Core::getTable('Account')->getChartMonthsTons($month,"Trash");
        $this->strXML .= "<dataset seriesName='Corporate Trash Volume' color='F1ACAC' anchorBorderColor='F1ACAC' anchorBgColor='F1ACAC'>";
        foreach ($chartCorporateTrash as $child) :
            $this->strXML .= "<set value='".number_format($child,2,'.','')."'/>";
        endforeach;
        $this->strXML .= "</dataset>";
    endif;

    $this->strXML .= "<styles><definition><style name='CaptionFont' type='font' size='12'/></definition><application><apply toObject='CAPTION' styles='CaptionFont'/><apply toObject='SUBCAPTION' styles='CaptionFont'/></application></styles>";
    $this->strXML .= "</chart>";

    return $this->renderPartial('invoice/individualChart',
            array('month' => $month, 'strXML' => $this->strXML,
                  'my' => $my, 'corporate' => $corporate  ));
  }

  public function executeReporting(sfWebRequest $request)
  {
    $this->locations = new Account();

    $this->pager = new sfDoctrinePager('Account',sfConfig::get('app_max_locations'));
    $this->pager->setQuery($this->locations->getChildsAccountQuery($this->getUser()->getAttribute('account_id')));
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();

    $this->locations = $this->pager->getResults();

    $this->account = Doctrine_Core::getTable('Account')->find($this->getUser()->getAttribute('account_id'));

    $this->childs_menu = Doctrine_Core::getTable('Account')->getChildsMenuAccountArray($this->getUser()->getAttribute('account_id'));

    $childs = Doctrine_Core::getTable('Account')->getChildsAccountArray($this->getUser()->getAttribute('account_id'));

    $this->strXML = "<chart caption='Tons per month' subcaption='(from ".date('m/d/Y', strtotime(date('Y').'/'.date('m').'/01 -5 month'))." to ".date('m/d/Y').")' xAxisName='Months' yAxisName='Tons' lineThickness='1' showValues='0' formatNumberScale='0' anchorRadius='2' divLineAlpha='20' divLineColor='CC3300' divLineIsDashed='1' showAlternateHGridColor='1' alternateHGridAlpha='5' alternateHGridColor='000000' shadowAlpha='40' labelStep='1' numvdivlines='4' chartRightMargin='35' bgColor='FFFFFF,000000' bgAngle='270' bgAlpha='10,10'>";
    
    $this->strXML .= "<categories>";
    for ($i=6; $i > 0; $i--) :
        $this->strXML .= "<category label='".date('M/Y', strtotime(date("Y-m-d"). '-'.($i-1).' month'))."'/>";
    endfor;

    $arrayMonths = Rubicon::arrayMonths(6);

    $this->strXML .= "</categories>";
    // Trash
    $chartTrash = Doctrine_Core::getTable('Account')->getChartMonthsTons2(6,"Trash",$childs);
    $this->strXML .= "<dataset seriesName='Trash Volume' color='1D8BD1' anchorBorderColor='1D8BD1' anchorBgColor='1D8BD1'>";
    //if ($chartTrash) :
    /*$i = 6;
    $date = date('m' , strtotime(date('m').' -'.(5).' month'));*/
    $i = 0;
    foreach ($chartTrash as $child) :
        if ($child->getMes() != $arrayMonths[$i]) :

            while (($arrayMonths[$i] != $child->getMes()) && ($i < 6)) :
                $this->strXML .= "<set value='".number_format(0,2,'.','')."'/>";
                $i++;
            endwhile;
        endif;

        $this->strXML .= "<set value='".number_format($child->getSuma(),2,'.','')."'/>";
        $i++;
    endforeach;
    //else :
    if ($i < 6) :
        while ($i < 6) :
            $this->strXML .= "<set value='".number_format(0,2,'.','')."'/>";
            $i++;
        endwhile;
    endif;
    //endif;

    $this->strXML .= "</dataset>";

    $chartRecycle = Doctrine_Core::getTable('Account')->getChartMonthsTons2(6,"Recycle",$childs);
    $this->strXML .= "<dataset seriesName='Recycle Volume' color='F1683C' anchorBorderColor='F1683C' anchorBgColor='F1683C'>";

    $i = 0;
    foreach ($chartRecycle as $child) :
        if ($child->getMes() != $arrayMonths[$i]) :

            while (($arrayMonths[$i] != $child->getMes()) && ($i < 6)) :
                $this->strXML .= "<set value='".number_format(0,2,'.','')."'/>";
                $i++;
            endwhile;
        endif;

        $this->strXML .= "<set value='".number_format($child->getSuma(),2,'.','')."'/>";
        $i++;
    endforeach;
    //else :
    if ($i < 6) :
        while ($i < 6) :
            $this->strXML .= "<set value='".number_format(0,2,'.','')."'/>";
            $i++;
        endwhile;
    endif;
    //if ($chartRecycle) :
       /* foreach ($chartRecycle as $child) :
            $this->strXML .= "<set value='".number_format($child,2,'.','')."'/>";
        endforeach;*/
    /*else :
        for ($i=6; $i > 0; $i--) :
            $this->strXML .= "<set value='".number_format(0,2,'.','')."'/>";
        endfor;
    endif;*/
    $this->strXML .= "</dataset>";
    //$this->strXML .= "<dataset seriesName='Score' color='AC8CD1' anchorBorderColor='AC8CD1' anchorBgColor='AC8CD1'><set value='10.11'/><set value='20.02'/><set value='10.11'/><set value='15.11'/><set value='5'/><set value='7.52'/><set value='13.99'/></dataset>";
    //$this->strXML .= "<dataset seriesName='Groups Trash Volume' color='F1C83C' anchorBorderColor='F1C83C' anchorBgColor='F1C83C'><set value='2'/><set value='3.21'/><set value='2.99'/><set value='3.15'/><set value='4.54'/><set value='5.76'/><set value='8.62'/></dataset>";

    $chartCorporateTrash = Doctrine_Core::getTable('Account')->getChartMonthsTons2(6,"Trash");
    $this->strXML .= "<dataset seriesName='Corporate Trash Volume' color='F1ACAC' anchorBorderColor='F1ACAC' anchorBgColor='F1ACAC'>";

    $i = 0;
    foreach ($chartCorporateTrash as $child) :
        if ($child->getMes() != $arrayMonths[$i]) :

            while (($arrayMonths[$i] != $child->getMes()) && ($i < 6)) :
                $this->strXML .= "<set value='".number_format(0,2,'.','')."'/>";
                $i++;
            endwhile;
        endif;

        $this->strXML .= "<set value='".number_format($child->getSuma(),2,'.','')."'/>";
        $i++;
    endforeach;
    //else :
    if ($i < 6) :
        while ($i < 6) :
            $this->strXML .= "<set value='".number_format(0,2,'.','')."'/>";
            $i++;
        endwhile;
    endif;

    /*foreach ($chartCorporateTrash as $child) :
        $this->strXML .= "<set value='".number_format($child,2,'.','')."'/>";
    endforeach;*/
    $this->strXML .= "</dataset>";

    $this->strXML .= "<styles><definition><style name='CaptionFont' type='font' size='12'/></definition><application><apply toObject='CAPTION' styles='CaptionFont'/><apply toObject='SUBCAPTION' styles='CaptionFont'/></application></styles>";
    $this->strXML .= "</chart>";
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->invoice = Doctrine_Core::getTable('Invoice')->find(array($request->getParameter('invoice_id')));
    $this->forward404Unless($this->invoice);

    $invoices = new Invoice();

    $this->invoices = array();
    $this->invoice_group = $this->invoice->getInvoiceGroup();

    if($this->getUser()->getAttribute('is_individual') == true)
    {
        $this->invoices[] = $this->invoice->getInvoiceId();
    }
    else
    {
        $res = Doctrine_Core::getTable('Invoice')->findByInvoiceGroup($this->invoice->getInvoiceGroup());
        //$q = "SELECT * FROM invoice WHERE invoice_group='".$invoiceData['invoice_group']."' AND invoice_deleted='no'";
        //$res = $db->query(Database::SELECT, $q);
        foreach($res as $inv)
        {
            $this->invoices[] = $inv->getInvoiceId();
        }
    }

    $this->invoice_num_display = $this->invoice->getInvoiceGroup();

    $this->multiInvoices = (count($this->invoices) > 1);

    $this->account = Doctrine_Core::getTable('Account')->find($this->invoice->getAccountId());

    if ($this->account->getAccountId()!=$this->getUser()->getAttribute('account_id')) :
         $this->redirect('default/secure');
    endif;

    //This two it could be directly in the template but I have to look if if it might change dinamicaly
    $this->runningBenchmark = $this->invoice->getInvoiceBenchmark();
    $this->hauler_pricing = $this->invoice->getInvoiceHaulerCost();

    $this->runningBenchmark = 0;
    $this->runningNew = 0;
    $this->runningHauler = 0;
    $this->rubiconFee = 0;
    $this->totalCreditAmount = 0;

    //Remember really is $invoices an array with one or more invoice's ids
    $this->services = Invoice::getServices($this->invoices);
    
  }

  public function executePrint(sfWebRequest $request)
  {
    $this->invoice = Doctrine_Core::getTable('Invoice')->find(array($request->getParameter('invoice_id')));
    $this->forward404Unless($this->invoice);

    $invoices = new Invoice();

    $this->invoices = array();
    $this->invoice_group = $this->invoice->getInvoiceGroup();

    if($this->getUser()->getAttribute('is_individual') == true)
    {
        $this->invoices[] = $this->invoice->getInvoiceId();
    }
    else
    {
        $res = Doctrine_Core::getTable('Invoice')->findByGroupId($this->invoice->getInvoiceGroup());
        //$q = "SELECT * FROM invoice WHERE invoice_group='".$invoiceData['invoice_group']."' AND invoice_deleted='no'";
        //$res = $db->query(Database::SELECT, $q);
        foreach($res as $inv)
        {
            $this->invoices[] = $inv->getInvoiceId();
        }
    }

    $this->invoice_num_display = $this->invoice->getInvoiceGroup();

    $this->multiInvoices = (count($this->invoices) > 1);

    $this->account = Doctrine_Core::getTable('Account')->find($this->invoice->getAccountId());

    if ($this->account->getAccountId()!=$this->getUser()->getAttribute('account_id')) :
         $this->redirect('default/secure');
    endif;

    //This two it could be directly in the template but I have to look if if it might change dinamicaly
    $this->runningBenchmark = $this->invoice->getInvoiceBenchmark();
    $this->hauler_pricing = $this->invoice->getInvoiceHaulerCost();

    $this->runningBenchmark = 0;
    $this->runningNew = 0;
    $this->runningHauler = 0;
    $this->rubiconFee = 0;
    $this->totalCreditAmount = 0;

    //Remember really is $invoices an array with one or more invoice's ids
    $this->services = Invoice::getServices($this->invoices);

  }
  
  public function executePdf(sfWebRequest $request)
  {
    $this->invoice = Doctrine_Core::getTable('Invoice')->find(array($request->getParameter('invoice_id')));
    $this->forward404Unless($this->invoice);

    $invoices = new Invoice();

    $this->invoices = array();
    $this->invoice_group = $this->invoice->getInvoiceGroup();

    if($this->getUser()->getAttribute('is_individual') == true)
    {
        $this->invoices[] = $this->invoice->getInvoiceId();
    }
    else
    {
        $res = Doctrine_Core::getTable('Invoice')->findByGroupId($this->invoice->getInvoiceGroup());
        //$q = "SELECT * FROM invoice WHERE invoice_group='".$invoiceData['invoice_group']."' AND invoice_deleted='no'";
        //$res = $db->query(Database::SELECT, $q);
        foreach($res as $inv)
        {
            $this->invoices[] = $inv->getInvoiceId();
        }
    }

    $this->invoice_num_display = $this->invoice->getInvoiceGroup();

    $this->multiInvoices = (count($this->invoices) > 1);

    $this->account = Doctrine_Core::getTable('Account')->find($this->invoice->getAccountId());

    if ($this->account->getAccountId()!=$this->getUser()->getAttribute('account_id')) :
         $this->redirect('default/secure');
    endif;

    //This two it could be directly in the template but I have to look if if it might change dinamicaly
    $this->runningBenchmark = $this->invoice->getInvoiceBenchmark();
    $this->hauler_pricing = $this->invoice->getInvoiceHaulerCost();

    $this->runningBenchmark = 0;
    $this->runningNew = 0;
    $this->runningHauler = 0;
    $this->rubiconFee = 0;
    $this->totalCreditAmount = 0;

    //Remember really is $invoices an array with one or more invoice's ids
    $this->services = Invoice::getServices($this->invoices);

    $htmlcontent = myUtils::getHtmlContent($this);
    $mpdf = new mPDF('en-GB','Letter','','',17,17,10,10,9,9);
    $mpdf->useOnlyCoreFonts = true;

    // LOAD a stylesheet
    $stylesheet = file_get_contents(sfConfig::get('sf_web_dir').'/css/mpdfstyletables.css');
    
    $mpdf->WriteHTML($stylesheet,1);   // The parameter 1 tells that this is css/style only and no body/html/text
    $mpdf->WriteHTML($htmlcontent, 2);
    $mpdf->Output('invoice_'.$this->invoice->getInvoiceGroup().'.pdf','D');
  }


  public function executeInvoice(sfWebRequest $request)
  {
    $this->invoice = Doctrine_Core::getTable('Invoice')->find(array($request->getParameter('invoice_id')));
    $this->forward404Unless($this->invoice);
    
    $this->invoices = array();
    $this->invoice_group = $this->invoice->getInvoiceGroup();

    if($this->getUser()->getAttribute('is_individual') == true)
    {
        $this->invoices[] = $this->invoice->getInvoiceId();
    }
    else
    {
        $res = Doctrine_Core::getTable('Invoice')->findByGroupId($this->invoice->getInvoiceGroup());
        //$q = "SELECT * FROM invoice WHERE invoice_group='".$invoiceData['invoice_group']."' AND invoice_deleted='no'";
        //$res = $db->query(Database::SELECT, $q);
        foreach($res as $inv)
        {
            $this->invoices[] = $inv->getInvoiceId();
        }
    }

    $this->invoice_num_display = $this->invoice->getInvoiceGroup();

    // Get the main account_id and use it for accountData, then get a list of all subaccounts
    $this->account_id = $this->invoice->getAccountId();
    $this->account = Doctrine_Core::getTable('Account')->find($this->account_id);

    if($this->account->getParentId() > 0)
    {
        $temp_account_id = $accountData['parent_id'];
        while($temp_account_id > 0)
        {
            $q = "SELECT * FROM account WHERE account_id='$temp_account_id'";
            $accountData = $db->query(Database::SELECT, $q)->current();
            if($accountData['parent_id'] > 0)
            {
                $temp_account_id = $accountData['parent_id'];
            }
            else
            {
                $temp_account_id = 0;
                $account_id = $accountData['account_id'];
            }
        }
    }
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new InvoiceForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new InvoiceForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($invoice = Doctrine_Core::getTable('Invoice')->find(array($request->getParameter('invoice_id'))), sprintf('Object invoice does not exist (%s).', $request->getParameter('invoice_id')));
    $this->form = new InvoiceForm($invoice);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($invoice = Doctrine_Core::getTable('Invoice')->find(array($request->getParameter('invoice_id'))), sprintf('Object invoice does not exist (%s).', $request->getParameter('invoice_id')));
    $this->form = new InvoiceForm($invoice);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($invoice = Doctrine_Core::getTable('Invoice')->find(array($request->getParameter('invoice_id'))), sprintf('Object invoice does not exist (%s).', $request->getParameter('invoice_id')));
    $invoice->delete();

    $this->redirect('invoice/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $invoice = $form->save();

      $this->redirect('invoice/edit?invoice_id='.$invoice->getInvoiceId());
    }
  }
}
